﻿var SubAddRowID = "subAdd";
var SubTemplateRowID = "subAdd";
var SubHeadRowID = "subHead";
var SubDeletesHiddenControlsID = "subDeleteIDs";
var SubTableName = "subTable";
var SubDataHiddenControlsID = "subListDatas";

var SubPKName = "GID";
var SubInfoName = "TStockDetailInfo";
var IFSubRowCanEdit = true;

function InitSubTable(subInfoName) {
    var rowHTML = $("#" + SubTemplateRowID).html();
    for (var subInfo in SubListData) {
        if (typeof SubListData[subInfo] != "function") {
            var gid = SubListData[subInfo][SubPKName];
            var jq = $(CreateInitRowHTML(rowHTML, SubListData[subInfo], gid));

            $("#" + SubAddRowID).before(jq);
            if (IFSubRowCanEdit)
                InitSubRowControls(jq, SubListData[subInfo], gid);
            // 扩展部分           
            if (typeof OnSubRowInitExtend != 'undefined') {
                OnSubRowInitExtend(gid, SubListData[subInfo], jq);
            }
        }
    }
}

function InitNotValidates(containerID) {
    if (typeof NotValidateInputs == 'undefined')
        NotValidateInputs = new Array();
    for (var input in HTMLInputTypes) {
        if (typeof HTMLInputTypes[input] != 'function')
            $("#" + containerID).find(HTMLInputTypes[input]).each(function () {
                var inputID = $(this).attr("id");
                if ($(this).attr("type") != "button") {
                    NotValidateInputs[inputID] = inputID;
                }
            });
    }
}
function GetInitSubRowHTML(templateHTML, gid) {
    var result = templateHTML;
    result = result.replace(/Add_/g, gid + "_");
    result = "<tr id='" + gid + "'>" + result + "</tr>";
    result = result.replace(App_Add, App_Delete).replace("AddSubRow();", "DeleteRowByID('" + gid + "');");
    return result;
}
function CreateInitRowHTML(templateHTML, rowData, gid) {
    return GetInitSubRowHTML(templateHTML, gid);
}

function CreateSubRowHTML(templateHTML, rowData, gid) {
    var result = GetInitSubRowHTML(templateHTML, gid);
    for (var item in rowData) {
        if (typeof rowData[item] != 'function') {
            var oldValue = "/" + SubInfoName + "_" + item + "/g";
            var data = rowData[item];
            if (typeof OnCreateRowItemHTMLExtend)
                data = OnCreateRowItemHTMLExtend(item, data);
            result = result.replace(eval(oldValue), data);
        }
    }
    return result;
}

function InitSubRowControls(jqSubRow, subInfo, gid) {
    for (var field in subInfo) {
        if (typeof subInfo[field] != 'function') {
            var peoperty = gid + "_" + SubInfoName + "_" + field;
            var data = subInfo[field];
            if (typeof data == 'undefined' || data == null || data == 'null')
                data = '';
            var box = jqSubRow.find("#" + peoperty);
            box.val(data);
        }
    }
}

function DeleteRowByID(id) {
    if (confirm(App_ConFirmDelete)) {
        $("#" + id).remove();
        $("#" + SubDeletesHiddenControlsID).val($("#" + SubDeletesHiddenControlsID).val() + "," + id);
        if (typeof OnSubDeleteExtend != 'undefined') {
            OnSubDeleteExtend(id);
        }
    }
}

function AddSubRowSubmit() {
    var templateRowHTML = $("#" + SubTemplateRowID).html();
    var gid = NewGuid();
    var currentRow = GetInitSubRowHTML(templateRowHTML, gid);
    var jq = $(currentRow);

    $("#" + SubAddRowID).before(jq);
    for (var input in HTMLInputTypes) {
        if (typeof HTMLInputTypes[input] != 'function') {
            $("#" + SubAddRowID).find(HTMLInputTypes[input]).each(function () {
                if ($(this).attr("type") != "button") {
                    var id = $(this).attr("id").replace("Add_", gid + "_");
                    jq.find("#" + id).val($(this).val());
                    $(this).val("");
                }
            });
        }
    }
    //扩展部分
    if (typeof OnAddSubRowSubmitExtend != 'undefined') {
        OnAddSubRowSubmitExtend(gid, null, jq);
    }
}

function AddSubRow() {
    async = false;
    var result = true;
    result = ValidateInContainer(SubAddRowID, function () { result = false }, true);
    if (result && typeof OnSubRowAddValidateExtend != 'undefined')
        result = OnSubRowAddValidateExtend();
    if (result) {
        AddSubRowSubmit();
        //扩展部分
        if (typeof OnAddSubRowSubmitedExtend != 'undefined')
            OnAddSubRowSubmitedExtend();
    }
    return result;
}

function OnSubmitExtend() {
    var submitData = [];
    $("#" + SubTableName + " tr").each(function () {
        var gid = $(this).attr("id");
        if (typeof gid != 'undefined' && gid != SubHeadRowID && gid != SubTemplateRowID && gid != "") {
            var sSubRowData = '"' + SubPKName + '":"' + gid + '"';
            for (var input in HTMLInputTypes) {
                if (typeof HTMLInputTypes[input] != 'function')
                    $(this).find(HTMLInputTypes[input]).each(function () {
                        if ($(this).attr("type") != "button") {
                            var fieldName = $(this).attr("id").replace(gid + "_" + SubInfoName + "_", "").replace("Add_" + SubInfoName + "_", "");
                            if ($(this).attr("type") == "checkbox") {
                                var ress = "false";
                                if ($(this).attr("checked") == "checked" || $(this).attr("checked") == "true")
                                    ress = true;
                                sSubRowData += ',"' + fieldName + '":"' + ress + '"';
                            }
                            else {
                                sSubRowData += ',"' + fieldName + '":"' + $(this).val() + '"';
                            }
                        }
                    });
            }
            submitData.push(JSON.parse('{' + sSubRowData + '}'));
        }
    });
    $("#" + SubDataHiddenControlsID).val(JSON.stringify(submitData));
    if (typeof OnSubEditSubmitExtend != 'undefined') {
        OnSubEditSubmitExtend();
    }
}
